package com.yanqu.road.server.cmd.activity.xingyoushanshui;

import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@CmdApi(reqMsg = XingYouShanShuiProto.ShanShuiTowerDeclareReq.class, respMsg = XingYouShanShuiProto.ShanShuiTowerDeclareRsp.class)
@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_TOWER_DECLARE, desc = "妈祖庙宣言设置")
public class ShanShuiTowerDeclareCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        XingYouShanShuiProto.ShanShuiTowerDeclareReq req = XingYouShanShuiProto.ShanShuiTowerDeclareReq.parseFrom(packet.getBytes());
        String declare = req.getDeclare();
        String checkName = StringUtils.clearBlankSpace(declare);
        if(StringUtils.isNullOrEmpty(checkName)){
            ret = GameErrorCode.E_FORBIDDEN_WORD;
            return;
        }
        //校验队伍名称长度
        if (checkName.length() > config.getSHANSHUI_DECLARATION_NUM()) {
            ret = GameErrorCode.E_XING_YOU_SHAN_SHUI_LENGTH_OVER;
            return;
        }
        //敏感字过滤
        if(ForbiddenWordMgr.isNickNameForbidden(checkName)){
            ret = GameErrorCode.E_FORBIDDEN_WORD;
            return;
        }
        XingYouShanShuiProto.ShanShuiTowerDeclareReq.Builder crossReq = XingYouShanShuiProto.ShanShuiTowerDeclareReq.newBuilder();
        crossReq.setActivityId(config.getActivityInfo().getActivityId());
        crossReq.setDeclare(checkName);
        crossReq.setTeamId(req.getTeamId());
        player.sendUnionActivityGroupPacket(Protocol.C_XING_YOU_SHAN_SHUI_TOWER_DECLARE, crossReq, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_TOWER_DECLARE, XingYouShanShuiProto.ShanShuiTowerDeclareRsp.newBuilder().setRet(ret));
        }
    }
}
